package com.booksystem.campuslibrarymanagementsystem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.BookBorrowrequst.Approval;
import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.ReservaTable;
import com.booksystem.campuslibrarymanagementsystem.commen.result.userresuilt.ReservationTablelist;
import com.booksystem.campuslibrarymanagementsystem.entity.BooksTable;
import com.booksystem.campuslibrarymanagementsystem.entity.RenewalapprovalTable;
import com.booksystem.campuslibrarymanagementsystem.entity.ReservationTable;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface ReservationTableMapper extends BaseMapper<ReservationTable> {

    //进行预约
    @Insert("INSERT INTO reservation_table (bookid, reservationdate, account, status) VALUES (#{bookid}, #{reservationdate}, #{account}, #{status})")
    int add(ReservationTable bb);
    //判断是否重复预约
    @Select("SELECT * \n" +
            "FROM reservation_table \n" +
            "WHERE account = #{account} AND bookid = #{bookid}")
    List<ReservationTable> existsByAccountAndBookidi(String account, int bookid);

    //判断预约图书是否存在
    @Select("SELECT * \n" +
            "FROM books_table \n" +
            "WHERE  bookid = #{bookid}")
    BooksTable aaa(int bookid);


    //预约用户id查看所有列表
    @Select("select * from reservation_table where account = #{account}")
    List<ReservationTablelist> appointlist(@Param("account") String account);

    //通过状态是否通过查看列表
    @Select("select * from reservation_table where account = #{account} and status = #{status}")
    List<ReservationTablelist> appointliststatus(String account,String status);

    //输入账户id,图书id修改状态列表

   // int cancelReservationById(int reservationid);
    @Update("UPDATE reservation_table SET status = '用户取消' WHERE account = #{account} AND reservationid = #{reservationid}")
    int updatestatus(String account, int reservationid, String status);

    //删除该账户的预约记录
    @Delete("DELETE FROM reservation_table WHERE account = #{account} AND reservationid = #{reservationid}")
    int deleteByAccountAndBookid(@Param("account") String account, @Param("reservationid") int reservationid);
    //通过图书id查询图书表，获得借出数量和总数
    @Select("SELECT * FROM books_table\n" +
            "WHERE bookid =  #{bookid} ")
    ReservaTable queryid(int bookid);

    //通过账户id查询借阅记录，获得该账户借阅记录
    @Select("SELECT * FROM reservation_table\n" +
            "WHERE account =  #{account} ")
    List<ReservationTable> queryByAccount(String account);

    //通过借阅审批成功，更新借阅审批表的该审批审批的状态，时间和日期
    @Update("UPDATE reservation_table SET approvaldate = #{approvalTime}, status = '通过' WHERE account = #{account} And bookid = #{bookid}")
    void onapproval(@Param("account") String account, @Param("approvalTime") LocalDate approvalTime , @Param("bookid") String bookid,@Param("account") String Approvalstate);
    //通过借阅审批失败，更新借阅审批表的该审批审批的状态，时间和日期
    @Update("UPDATE reservation_table SET approvaldate = #{approvalTime}, status = '不通过' WHERE account = #{account} And bookid = #{bookid}")
    void unapproval(@Param("account") String account, @Param("approvalTime") LocalDate approvalTime , @Param("bookid") String bookid,@Param("account") String Approvalstate);    //    查询账户人员信息
    @Select("select * from reservation_table where status = #{state} ")
    List<ReservationTable> querydislist(String status);

}
